package demo;

class Food{

}
class Fruit extends Food{

}
class Apple extends Fruit{

}
class Banana extends Fruit{

}
class Plate<T>{
    private T message;
    public void setMessage(T message){
        this.message = message;
    }
    public T getMessage(){
        return message;
    }
}
public class Test {
    public static void main(String[] args) {
        Plate<Fruit> plate1 = new Plate<>();
        plate1.setMessage(new Apple());
        func1(plate1);
        Plate<Banana> plate2 = new Plate<>();
        plate2.setMessage(new Banana());
        Plate<Fruit> plate3 = new Plate<>();
        plate3.setMessage(new Fruit());
        func2(plate3);
    }
    //通配符的上界
    public static void func1(Plate<? extends Fruit> plate){//?只能为Fruit或者Fruit的子类
//        plate.setMessage(new Apple());//此时不能添加数据
        System.out.println(plate.getMessage());
    }
    //通配符的下界
    public static void func2(Plate<? super Fruit> plate){//?只能为Fruit或者Fruit的父类
        plate.setMessage(new Apple());//此时可以添加Fruit及Fruit的子类，但是不能添Fruit的父类对象
        plate.setMessage(new Fruit());
//        plate.setMessage(new Food());//不能添Fruit的父类对象
        System.out.println(plate.getMessage());
    }
}
